//
//  ProfileViewController.m
//  LoveFreshBeen
//
//  Created by 魏根 on 2016/10/15.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "ProfileViewController.h"
#import "ProfileButton.h"
#import "HeadView.h"
#import "MyorderTVC.h"
#import "ChargeTVC.h"
#import "MessageTVC.h"
#import <UMSocialCore/UMSocialCore.h>
#import "AdressTableViewController.h"
#import "MyStoreController.h"

@interface ProfileViewController ()

@property (weak, nonatomic) IBOutlet UIView *headView;
@property(nonatomic,weak) ProfileButton *myOrderBtn;
@property(nonatomic,strong) ProfileButton *submitBtn;
@property(nonatomic,weak) ProfileButton *myMessageBtn;

@end

static NSString *reuseId = @"tableviewCellId";
@implementation ProfileViewController


- (instancetype)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
        
        UIStoryboard *profile = [UIStoryboard storyboardWithName:@"Profile" bundle:nil];
        self = [profile instantiateInitialViewController];
        
    }
    return self;

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseId];
    [self setHeadView];
    [self creatHeaderButtons];
    [self setHeaderButtonsFrame];

}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
   
}
- (void)setHeadView {
    HeadView *headV = [[HeadView alloc] init];
    [self.headView addSubview:headV];
    [headV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self.headView).offset(-1);
        make.trailing.bottom.equalTo(self.headView).offset(1);
    }];
    
}

- (void) setNavigationBar {
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)creatHeaderButtons {
    ProfileButton *btn = [[ProfileButton alloc] init];
    self.myOrderBtn = btn;
    [self.headView addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"v2_my_order_icon"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"v2_my_order_icon2"] forState:UIControlStateHighlighted];
    [btn setTitle:@"我的订单" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(myOrderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    ProfileButton *b2 = [[ProfileButton alloc] init];
    self.submitBtn = b2;
    [self.headView addSubview:b2];
    [b2 setImage:[UIImage imageNamed:@"v2_submit_Icon"] forState:UIControlStateNormal];
    [b2 setTitle:@"优惠券" forState:UIControlStateNormal];
    [b2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [b2 setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
    [b2 addTarget:self action:@selector(mysubmitBtnClick) forControlEvents:UIControlEventTouchUpInside];

    ProfileButton *b3 = [[ProfileButton alloc] init];
    self.myMessageBtn = b3;
    [self.headView addSubview:b3];
    [b3 setImage:[UIImage imageNamed:@"v2_my_message_icon"] forState:UIControlStateNormal];
    [b3 setTitle:@"我的消息" forState:UIControlStateNormal];
    [b3 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [b3 setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
    [b3 addTarget:self action:@selector(myMessageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)setHeaderButtonsFrame {
    [self.myOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.headView);
        make.leading.equalTo(self.headView);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width / 3));
    }];
    [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.headView);
        make.left.equalTo(self.myOrderBtn.mas_right);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width / 3));
    }];
    [self.myMessageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.headView);
        make.leading.equalTo(self.submitBtn.mas_trailing);
        make.width.equalTo(self.myOrderBtn);
    }];
    
    
}

- (void)myOrderBtnClick {
  
    MyorderTVC *myorderTableViewController = [[MyorderTVC alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:myorderTableViewController animated:YES];
}

- (void)mysubmitBtnClick {
    ChargeTVC *charge = [[ChargeTVC alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:charge animated:YES];
}

- (void) myMessageBtnClick {
    MessageTVC *message = [[MessageTVC alloc] initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:message animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.1;
    }
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  NSLog(@"%ld----%ld",indexPath.section,indexPath.row);
    if(indexPath.section == 1 && indexPath.row == 0){
        AdressTableViewController* addressVC = [[AdressTableViewController alloc] init];
        [self.navigationController pushViewController:addressVC animated:YES];
    }
    
    //我的店铺
    if (indexPath.section == 1 && indexPath.row == 1) {
     
        MyStoreController *myStoreController = [[MyStoreController alloc]init];
        
        [self.navigationController pushViewController:myStoreController animated:YES];
    }
    
    if(indexPath.section == 2 && indexPath.row == 0){
        
        UIAlertController *sheetView = [UIAlertController alertControllerWithTitle:@"分享到" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *alert1 = [UIAlertAction actionWithTitle:@"微信好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSLog(@"========哈哈哈哈哈");
        }];
        
        [sheetView addAction:alert1];
        
        UIAlertAction *alert2 = [UIAlertAction actionWithTitle:@"微信圈好友" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self getUserInfoFromSina];
        }];
        
        [sheetView addAction:alert2];
        
        UIAlertAction *alert3 = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self shareTextToSina];
            
        }];
        
        [sheetView addAction:alert3];
        
        UIAlertAction *alert4 = [UIAlertAction actionWithTitle:@"QQ空间" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
             [self getAuthInfoFromSina];
        }];
        
        [sheetView addAction:alert4];
        
        UIAlertAction *alert5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [sheetView addAction:alert5];
        
        [self presentViewController:sheetView animated:YES completion:nil];
        
    }
}


- (ProfileButton *)submitBtn {
    if (_submitBtn == nil) {
        _submitBtn = [[ProfileButton alloc] init];
        
    }
    return _submitBtn;
}

//新浪分享

-(void)getAuthInfoFromSina
{
    [[UMSocialManager defaultManager]  authWithPlatform:UMSocialPlatformType_Sina currentViewController:self completion:^(id result, NSError *error) {
        
        UMSocialAuthResponse *authresponse = result;
        NSString *message = [NSString stringWithFormat:@"result: %d\n uid: %@\n accessToken: %@\n",(int)error.code,authresponse.uid,authresponse.accessToken];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Login" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}

- (void)getUserInfoFromSina
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_Sina currentViewController:self completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *userinfo =result;
        NSString *message = [NSString stringWithFormat:@"name: %@\n icon: %@\n gender: %@\n",userinfo.name,userinfo.iconurl,userinfo.gender];

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"UserInfo" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}

- (void)shareTextToSina
{
    NSString *text = @"神呀,救救我吧,一把年纪了,还是一个人!....";
    
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    messageObject.text = text;
    
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        NSString *message = nil;
        if (!error) {
            message = [NSString stringWithFormat:@"分享成功"];
        } else {
            message = [NSString stringWithFormat:@"失败原因Code: %d\n",(int)error.code];
            
        }

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"share" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        
    }];
}


@end



